import config from '@/config/index';
import axios from 'axios';
import qs from 'qs';

//切换环境
if (process.env.NODE_ENV === 'development') {
    axios.defaults.baseURL = config.baseUrl;
}
axios.defaults.timeout = 3000; //请求超时时间
axios.defaults.withCredentials = true;

//封装get方法
export function get(url, params = null) {
    return new Promise((resolve, reject) => {
        axios.get(url, {
            params: params
        }).then(res => {
            resolve(res);
        }).catch(err => {
            reject(err);
        });
    });
}

//封装post方法
export function post(url, params, type = 'form-data') {
    return new Promise((resolve, reject) => {
        if (type === 'form-data') {
            axios.post(url, qs.stringify(params))
                .then(res => {
                    resolve(res);
                })
                .catch(err => {
                    reject(err);
                });
        } else {
            axios.post(url, params)
                .then(res => {
                    resolve(res);
                })
                .catch(err => {
                    reject(err);
                });
        }
    });
}
